package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.allservice.entity.Type;
import com.cjq.allservice.entity.vo.TypeQuery;
import com.cjq.allservice.service.TypeService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/allservice/type")
@CrossOrigin
public class TypeController {

    @Autowired
    TypeService typeService;

    @ApiOperation("获取所有商品类型")
    @GetMapping("/getAllType")
    public R getAllType(){
        List<Type> list = typeService.list();
        return R.succ().data("typeList",list);
    }

    @ApiOperation("使用分页方式返回所有的商品类型")
    @PostMapping("/getAllTypeWithPage/{current}/{limit}")
    public R getAllTypeWithPage(@PathVariable long current,@PathVariable long limit){
        Page<Type> page=new Page<>(current,limit);
        typeService.page(page,null);
        long total=page.getTotal();
        List<Type> records = page.getRecords();
        HashMap map=new HashMap();
        map.put("total",total);
        map.put("typeList",records);
        return  R.succ().data(map);
    }

    @ApiOperation("添加一个商品类型")
    @PostMapping("/addType")
    public R addType(@RequestBody Type type){
        boolean save = typeService.save(type);
        if (save==true){
            return R.succ().message("添加成功！");
        }else {
            return R.fail().message("添加失败！");
        }
    }

    @ApiOperation("删除商品类型")
    @PostMapping("/deleteType")
    public R deleteType(@RequestParam String id){
        boolean b = typeService.removeById(id);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败！");
        }
    }

    @ApiOperation("修改商品类型信息")
    @PostMapping("/updateType")
    public R updateType(@RequestBody Type type){
        boolean b = typeService.updateById(type);
        if (b==true){
            return R.succ().message("修改成功！");
        }else {
            return R.fail().message("修改失败！");
        }
    }

    @ApiOperation("根据id进行商品类型的回显")
    @GetMapping("/getTypeById")
    public R getTypeById(@RequestParam String id){
        Type type = typeService.getById(id);
        return R.succ().data("typeInfo",type);
    }


    @ApiOperation("修改商品类型的状态")
    @PostMapping("/updateTypeStatusById")
    public R updateTypeStatusById(@RequestParam String id){
        Type type = typeService.getById(id);
        if (type.getTypeStatus()==1){
            type.setTypeStatus(0);
        }else{
            type.setTypeStatus(1);
        }
        boolean b = typeService.updateById(type);
        if (b==true){
            return R.succ().message("修改状态成功!");
        }else{
            return R.fail().message("修改状态失败！");
        }
    }


    @ApiOperation("获取所有父类类型")
    @GetMapping("/getParentType")
    public R getParentType(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type_parentId",0);
        List list = typeService.list(wrapper);
        return R.succ().data("typeParentList",list);
    }

    @ApiOperation("根据父类id获取子类列表数据")
    @GetMapping("/getTypeByParentId")
    public R getTypeByParentId(@RequestParam String parentId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type_parentId",parentId);
        List list = typeService.list(wrapper);
        return R.succ().data("typeList",list);
    }

    @ApiOperation("使用树形结构返回商品类目")
    @GetMapping("/getAllTypeWithTree")
    public R getAllTypeWithTree(){
        List<Type> list=typeService.listWithTree();
        return R.succ().data("list",list);
    }

    @ApiOperation("条件查询类目带分页")
    //条件查询带分页
    @PostMapping ("pageTypeCondition/{current}/{limit}")
    public R pageDoctorsCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TypeQuery typeQuery){
        Page<Type> page=new Page<>(current,limit);
        QueryWrapper<Type> wrapper = new QueryWrapper<>();

        //多条件组合查询
        String id = typeQuery.getId();
        String name = typeQuery.getName();
        String status = typeQuery.getStatus();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("type_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("type_name",name);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("type_status",status);
        }
        wrapper.orderByDesc("update_time");

        typeService.page(page,wrapper);
        long total=page.getTotal();
        System.out.println(total);
        List<Type> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.succ().data(map);
    }




}

